import Vue from 'vue'
import {getToken} from '@/utils/index.js'

export default {
	ajax: function(options) {
		var defaults = {
			method: 'POST',
			url: "",
			data: "",
			success: ""
		};
		var $this = this
		var opts = this.replaceDf(defaults, options);
		uni.request({
			data: opts.data,
			method: opts.method,
			url: '/test/' + opts.url,
			header: {
				cache: 'no-cache',
				'content-type': 'application/json',
				token: getToken()
				//token: store.state.userInfo.token,
				// ...store.state.locationCityHeader,
				// channelId: config.channelId,
			},
			success(res) {
				if (res.statusCode === 200) {
					opts.success(res.data);
				} else {
					opts.success(res.data);
				}
			},
			fail(err) {
				opts.success(err);
			},
		})
	},
	goPage: function(url) {
		uni.navigateTo({
			url: url,
		});
		//navigateTo redirectTo
	},
	replaceDf: function(obj1, obj2) { //**把obj2中的的属性值赋值给obj1对应的属性
		for (var key in obj2) {
			obj1[key] = obj2[key]
		}
		return obj1;
	},
	setSessionStorage(Id, value) {
		if (value != null && Id != null) {
			sessionStorage.setItem(Id, JSON.stringify(value))
		}
	},
	getSessionStorageObject(Id) {
		var obj = sessionStorage.getItem(Id)
		if (obj != undefined) {
			return JSON.parse(obj);
		} else {
			return null
		}
	},
	getSessionStorageString(Id) {
		var str = sessionStorage.getItem(Id)
		if (str != undefined) {
			return str;
		} else {
			return null
		}
	},
	removeSsessionStorage(Id) {
		if (Id != undefined && Id != null && Id != '') {
			sessionStorage.removeItem(Id)
		}
	},
	setlocalStorageObject(Id, value) { //专门为支付打开新窗口使用
		if (value != null && Id != null) {
			localStorage.setItem(Id, JSON.stringify(value))
		}
	},
	getlocalStorageObject(Id) { //专门为支付打开新窗口使用
		var obj = localStorage.getItem(Id)
		if (obj != undefined) {
			return JSON.parse(obj)
		} else {
			return null
		}
	},
	getlocalStorageString(Id) {
		var str = localStorage.getItem(Id)
		if (str != undefined) {
			return str;
		} else {
			return null
		}
	},
	removelocalStorage(Id) { //专门为支付打开新窗口使用
		if (Id != undefined && Id != null && Id != '') {
			localStorage.removeItem(Id)
		}
	},

}
